﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace VisualGTest
{
    /// <summary>
    /// Represents information about a unit test failure.
    /// </summary>
    public class TestFailureInfo : INotifyPropertyChanged, IComparable
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region property backing variables
        private string _fileName;
        private int _lineNumber;
        private string _summary;
        private string _message;
        #endregion

        #region properties
        public string FileName
        {
            get
            {
                return _fileName;
            }
            set
            {
                if (_fileName != value)
                {
                    _fileName = value;
                    NotifyPropertyChanged("FileName");
                }
            }
        }

        /// <summary>
        /// Gets or sets the line number of the test failure in the test file.
        /// Initialized to -1.
        /// </summary>
        public int LineNumber
        {
            get
            {
                return _lineNumber;
            }
            set
            {
                if (_lineNumber != value)
                {
                    _lineNumber = value;
                    NotifyPropertyChanged("LineNumber");
                }
            }
        }

        public string Summary
        {
            get
            {
                return _summary;
            }
            set
            {
                if (_summary != value)
                {
                    _summary = value;
                    NotifyPropertyChanged("Summary");
                }
            }
        }

        /// <summary>
        /// Includes additional information beyond the Summary, for example
        /// stack traces.
        /// </summary>
        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                if (_message != value)
                {
                    _message = value;
                    NotifyPropertyChanged("Message");
                }
            }
        }
        #endregion

        public TestFailureInfo()
        {
            FileName = "";
            LineNumber = -1;
            Summary = "";
            Message = "";
        }

        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="failureInfo">the object to copy</param>
        public TestFailureInfo(TestFailureInfo other)
        {
            this.FileName = other.FileName;
            this.LineNumber = other.LineNumber;
            this.Summary = other.Summary;
            this.Message = other.Message;
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            TestFailureInfo other = obj as TestFailureInfo;

            if (other == null)
            {
                throw new ArgumentException("object is not a TestFailureInfo");
            }

            int result = this.FileName.CompareTo(other.FileName);

            if (result == 0)
            {
                result = this.LineNumber.CompareTo(other.LineNumber);

                if (result == 0)
                {
                    result = this.Summary.CompareTo(other.Summary);
                }
            }

            return result;
        }

        #endregion
    }
}
